package com.doyens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doyens.dto.DishDto;
import com.doyens.entity.Dish;
import com.doyens.entity.DishFlavor;
import com.doyens.mapper.DishMapper;
import com.doyens.service.DishFlavorService;
import com.doyens.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     *  新增菜品,同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);

        //MP 特性 ,  新增完以后, 自增后的id会给封装到对象的id中
        Long dishId = dishDto.getId();//菜品id
        //获取菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //保存到口味表里
        //需要把dishId 按个存到  flavors集合中的每个对象中取
        //以前的写法
        /*List<DishFlavor>  flavors1 =  new ArrayList<>();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            flavors1.add(flavor);
        }
        log.info("flavors======"+flavors.toString());
        log.info("flavors======"+flavors1.toString());*/

        //jdk1.8以后的特性, 效率会更高
        flavors=  flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存口味
        dishFlavorService.saveBatch(flavors);


    }

    //根据id查询菜品信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        //对象属性复制
        BeanUtils.copyProperties(dish,dishDto);
        //查询对应的口味信息
        LambdaQueryWrapper<DishFlavor>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;

    }

    /**
     * 修改菜品
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
            //更新dish表的基本信息
            this.updateById(dishDto);

            //针对dishFlavor口味表的信息, 先删除再添加操作
            //删除口味表中对应的菜品信息
            LambdaQueryWrapper<DishFlavor>  queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
            dishFlavorService.remove(queryWrapper);//删除操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //批量保存
        dishFlavorService.saveBatch(flavors);

    }
}
